package co.edu.unal.geditor.controller.action.undoable.color;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;

import co.edu.unal.geditor.model.ClosedGraphElement;
import co.edu.unal.geditor.model.GraphElement;

public class UndoableChangeFillingColorAction extends AbstractUndoableEdit {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public UndoableChangeFillingColorAction(ArrayList<GraphElement> changedElements) {
		for (GraphElement ge : changedElements){
			m_arrayChangedElements.put(ge, ge.getFillColor());
		}
		m_oldDefaultFillingColor=ClosedGraphElement.getDefaultFillingColor();
	}
	public void undo() throws CannotUndoException {
	
		
		for (GraphElement ge : m_arrayChangedElements.keySet()){
			ge.setFillColor(m_arrayChangedElements.get(ge));
		}
		m_newDefaultFillingColor=ClosedGraphElement.getDefaultFillingColor();
		ClosedGraphElement.setDefaultFillingColor(m_oldDefaultFillingColor);
	}

	public void redo() throws CannotRedoException {
		for (GraphElement ge : m_arrayChangedElements.keySet()){
			ge.setFillColor(m_newDefaultFillingColor);
		}
		ClosedGraphElement.setDefaultFillingColor(m_newDefaultFillingColor);
	}

	public boolean canUndo() {
		return (m_arrayChangedElements.size()>=0);
	}

	public boolean canRedo() {
		return (m_oldDefaultFillingColor!=null);
	}

	public String getPresentationName() {
		return "Change Filling Color";
	}
	private Color m_newDefaultFillingColor;
	private Color m_oldDefaultFillingColor;
	private Map<GraphElement,Color> m_arrayChangedElements = new HashMap<GraphElement,Color>();
}
